#include "compile_run.hpp"
#include "../comm/httplib.h"
using namespace ns_compile_and_run;
using namespace httplib;

// int main()
// {
//         // 下面的工作，充当客户端请求的json串
//     std::string in_json;
//     Json::Value in_value;
//     //R"()", raw string
//     //in_value["code"] = R"(#include<iostream>int main(){std::cout << "你可以看见我了" << std::endl;return 0;})";
//     //in_value["code"] = ("#include <iostream>\nint main(){std::cout<<\"你好，编译服务！\"<<std::endl;\nreturn 0;");
//     in_value["code"] = R"(#include<iostream>
//     int main(){
//         std::cout << "你可以看见我了" << std::endl;
//         return 0;
//         })";
//     in_value["input"] = "";
//     in_value["cpu_limit"] = 1;
//     in_value["mem_limit"] = 10240*3;
//     Json::FastWriter writer;
//     in_json = writer.write(in_value);
//     std::cout << in_json << std::endl;
//     //这个是将来给客户端返回的json串
//     std::string out_json;
//     std::cout << "调用start函数" << std::endl;
//     Compile_Run::start(in_json, &out_json);
//     std::cout << out_json << std::endl;
//     return 0;
// }

//补充 httplib库的基本使用

void Usage(std::string proc)
{
    std::cerr << "Usage: " << "\n\t" << proc << " port" << std::endl;
}

int main(int argc, char *argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        return 1;
    }
    Server svr;


    // svr.Get("/hello",[](const Request &req, Response &resp){
    // // 用来进行基本测试
    //     resp.set_content("hello,霍苏琦 ", "text/plain;charset=utf-8");
    // });
    // svr.Get("/hello",[](const Request &req,Response& resp){
    //     resp.set_content("hello httplib,你好 httplib!","test/plain;charset=utf-8");
    // });
    // svr.Post("/compile_and_run",[](const Request& req, Response &resp){
    //     std::string in_json=req.body;
    //     std::string out_json;
    //     if(!in_json.empty())
    //     {
    //         Compile_Run::start(in_json,&out_json);
    //         resp.set_content(out_json,"application/json;charset=utf-8");
    //     }
    // });

    svr.Post("/compile_and_run", [](const Request &req, Response &resp){
        // 用户请求的服务正文是我们想要的json string
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty())
        {
            Compile_Run::start(in_json, &out_json);
            resp.set_content(out_json, "application/json;charset=utf-8");
        }
    });
    svr.listen("0.0.0.0",atoi(argv[1]));
    return 0;
}  
